﻿using System;
using System.Collections.Generic;
using Antu.DataAccess;
using Antu.WorkFlowFactory;

namespace FlowProvider
{
    public sealed class FlowSqlDataProvider : FlowFactoryProvider
    {
        internal FlowSqlDataProvider()
        {
        }
        public override IDataAccess dataAccess(bool transaction)
        {
            //return DataFactory.GetInstance(transaction, DatabaseType.Oracle, base.ProviderProperties["ConnectionString"].ToString());
            return GetDicDatabase("FlowConnectionString", base.ProviderProperties["FlowConnectionString"].ToString(), transaction);
        }

        #region 实现动态重用联接
        static private Dictionary<string, IDataAccess> _dicDatabase = new Dictionary<string, IDataAccess>(StringComparer.InvariantCultureIgnoreCase);
        /// <summary>
        /// 实现动态重用联接
        /// </summary>
        /// <param name="name">联接名</param>
        /// <param name="value">联接值</param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public static IDataAccess GetDicDatabase(string name, string value, bool transaction)
        {
            IDataAccess dbTemp = null;
            lock (_dicDatabase)
            {
                if (_dicDatabase.ContainsKey(name))
                {
                    dbTemp = _dicDatabase[name];
                }
                else
                {
                    dbTemp = DataFactory.GetInstance(transaction, DatabaseType.Oracle, value);
                    _dicDatabase.Add(name, dbTemp);
                }
            }
            return dbTemp;
        }
        #endregion


    }
}
